package com.cskaoyan.service;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.MarketOrderGoods;
import com.cskaoyan.bean.MarketOrderGoodsExample;
import com.cskaoyan.bean.bo.AdminOrderListBO;
import com.cskaoyan.bean.bo.AdminOrderRefundBO;
import com.cskaoyan.bean.bo.AdminOrderShipBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminOrderChannelVO;
import com.cskaoyan.bean.vo.AdminOrderDetailVO;
import com.cskaoyan.bean.vo.AdminOrderDetailVO2;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * \* User: 余雨健
 * \* Date: 2022/10/12
 * \* Time: 23:13
 **/
@Service
public class AdminOrderServiceImpl implements AdminOrderService{

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public List<AdminOrderChannelVO> ShowChannel() {
        List<AdminOrderChannelVO> list = new ArrayList<>();
        list.add(new AdminOrderChannelVO("ZTO","中通快递"));
        list.add(new AdminOrderChannelVO("YTO","韵达快递"));
        list.add(new AdminOrderChannelVO("YD","韵达快递"));
        list.add(new AdminOrderChannelVO("YZPY","邮政快递包裹"));
        list.add(new AdminOrderChannelVO("EMS","EMS"));
        list.add(new AdminOrderChannelVO("DBL","德邦快递"));
        list.add(new AdminOrderChannelVO("FAST","快捷快递"));
        list.add(new AdminOrderChannelVO("ZJS","宅急送"));
        list.add(new AdminOrderChannelVO("TNT","TNT快递"));
        list.add(new AdminOrderChannelVO("UPS","UPS"));
        list.add(new AdminOrderChannelVO("DHL","DHL"));
        list.add(new AdminOrderChannelVO("FEDEX","FEDEX联邦(国内件)"));
        list.add(new AdminOrderChannelVO("FEDEX_GJ","FEDEX联邦(国际件)"));
        return list;
    }

    @Override
    public CommonData<MarketOrder> seleteOrderList(AdminOrderListBO adminOrderListBO) {
        Integer limit = adminOrderListBO.getLimit();
        Integer page = adminOrderListBO.getPage();
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if(adminOrderListBO.getTimeArray() != null && adminOrderListBO.getTimeArray().length > 0){
            criteria.andPayTimeBetween(adminOrderListBO.getTimeArray()[0],adminOrderListBO.getTimeArray()[1]);
        }
        if(adminOrderListBO.getUserId() != null){
            criteria.andUserIdEqualTo(adminOrderListBO.getUserId());
        }
        if(adminOrderListBO.getOrderSn() != null){
            criteria.andOrderSnEqualTo(adminOrderListBO.getOrderSn());
        }
        if(adminOrderListBO.getOrderStatusArray() != null){
            criteria.andOrderStatusEqualTo(adminOrderListBO.getOrderStatusArray()[0]);
        }
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        int total = marketOrders.size();
        int papes = (total + limit - 1) / limit;
        return new CommonData<MarketOrder>(total,papes,limit,page,marketOrders);
    }

    @Override
    public AdminOrderDetailVO selectOrderDetail(Integer id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        Integer userId = marketOrder.getUserId();
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        AdminOrderDetailVO2 adminOrderDetailVO2 = marketUserMapper.selectByPrimaryKeyByUserId(userId);
        return new AdminOrderDetailVO(marketOrder,marketOrderGoods,adminOrderDetailVO2);
    }

    @Override
    public void deleteOrder(Integer id) {
        marketOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void refundOrder(AdminOrderRefundBO adminOrderRefundBO) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        try {
            java.util.Date date1 = formatter.parse(Date);
            MarketOrder marketOrder = new MarketOrder();
            marketOrder.setRefundTime(date1);
            marketOrder.setOrderStatus((short) 203);
            marketOrder.setId(adminOrderRefundBO.getOrderId());
            marketOrder.setRefundAmount(adminOrderRefundBO.getRefundMoney());
            marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void shipOrder(AdminOrderShipBO adminOrderShipBO) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        try {
            java.util.Date date1 = formatter.parse(Date);
            MarketOrder marketOrder = new MarketOrder();
            marketOrder.setShipTime(date1);
            marketOrder.setShipSn(adminOrderShipBO.getShipSn());
            marketOrder.setShipChannel(adminOrderShipBO.getShipChannel());
            marketOrder.setId(adminOrderShipBO.getOrderId());
            marketOrder.setOrderStatus((short) 301);
            marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


}
